L.Group = L.Class.extend({
	includes: L.Mixin.Events,

	initialize: function(name, groupType, ownerId, couplingOptions) {
		
		this._id = uniqueId(this, true);
		this._name = name;
		this._groupType = groupType;
		this._ownerId = Number(ownerId);
		this._members = [ this._ownerId ];
		this._roomList = null;
		this._couplingOptions = (couplingOptions == null ? null : {
			enabled: couplingOptions.enabled,
			adapter: couplingOptions.adapter,
			carriedId: couplingOptions.carriedId + "-" + this._groupType + "group-" + this._name
		});
		
		if ( this._couplingOptions && this._couplingOptions.enabled ) {
			this._couple(this._couplingOptions);
			this._roomList = new L.RoomList("groupal", {
				enabled: this._couplingOptions.enabled,
				adapter: this._couplingOptions.adapter,
				carriedId: this._couplingOptions.carriedId + "-roomlist"
			});
		} else {
			this._roomList = new L.RoomList("groupal", null);
		}

		this._roomList.setGroup(this);
		this._roomList.on('roomAction', Skt.model.fireRoomActionProxy, Skt.model);
	},
	
	addMember: function(userId) {
//		console.log("this.isMember(userId): " + this.isMember(userId));
		if ( !this.isMember(userId) ) {
			this._members.push(Number(userId));
//			console.log("this.fire('memberEvent', {");
			this.fire('memberEvent', {
				action: 'addMember',
				userId: Number(userId),
				groupName: this._name,
				groupType: this._groupType
			});
		}
	},
	
	removeMember: function(userId) {
		if ( this.isMember(userId) ) {
			this._members.splice(this._members.indexOf(Number(userId)), 1);
			this.fire('memberEvent', { 
				action: 'removeMember',
				userId: Number(userId),
				groupName: this._name,
				groupType: this._groupType
			});
		}
	},
	
	invite: function(inviterId, invitedId) {
		if ( this._groupType == 'note'
			&& !this.isMember(invitedId)
			&& this.canInvite(inviterId) ) {
			
			var params = {
				name: this._name,
				groupType: this._groupType,
				ownerId: this._ownerId,
				inviterId: Number(inviterId),
				invitedId: Number(invitedId)
			}; 
			console.log('params');
			console.log(params);
			this.fire('invitationEvent', {
				name: this._name,
				groupType: this._groupType,
				ownerId: this._ownerId,
				inviterId: Number(inviterId),
				invitedId: Number(invitedId)
			});
			
		}
	},
	
	kick: function(kickerId, kickedId) {
		if ( this._groupType == 'note'
			&& this.isOwner(kickerId)
			&& this.isMember(kickedId) ) {
			this.removeMember(kickedId);
		}
	},
	
	isOwner: function(userId) {
		return this._ownerId == Number(userId);
	},
 	
	isMember: function(userId) {
		return $.inArray(Number(userId), this._members) >= 0;
	},
	
	canInvite: function(userId) {
		return this._groupType == "note" && this.isMember(userId);
	},
	
	hasTVMember: function() {
		for ( var k = 0; k < this._members.length; ++k )
			if( Skt.user_list.getUser(this._members[k]).rol == 'tv' )
				return true;
			
		return false;
	},
	
	getName: function() {
		return this._name;
	},
	
	getGroupType: function() {
		return this._groupType;
	},
	
	getOwnerId: function() {
		return this._ownerId;
	},
	
	getMembers: function() {
		return this._members;
	},
	
	getRoomList: function() {
		return this._roomList;
	},
	
	getId: function() {
		return this._id;
	},
	
	_couple: function(couplingOptions) {
		couplingOptions.adapter.coupleObject(
			couplingOptions.carriedId, 
			this,
			{
				messageType: "EVENT",
				explicitMapping: ["addMember", "removeMember"]
			}
		);
	}
	
});